package com.linxz.wandnroid.appui;

import android.annotation.SuppressLint;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.linxz.framework.base.BaseMVPFragment;
import com.linxz.framework.utils.ShareSDKManager;
import com.linxz.ui.GuiHelper;
import com.linxz.ui.dialogs.DialogHelper;
import com.linxz.ui.listener.WebSaveListener;
import com.linxz.wandnroid.R;
import com.linxz.wandnroid.adapter.GankListAdapter;
import com.linxz.wandnroid.appui.view.GankFragView;
import com.linxz.wandnroid.pojo.GankBean;
import com.linxz.wandnroid.pojo.GankMultiBean;
import com.linxz.wandnroid.pojo.WandroidProjectBean;
import com.linxz.wandnroid.presenter.GanFragPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Linxz
 * 创建日期：2020年02月28日 14:35
 * version：v4.5.4
 * 描述：
 */
public class GankFragment extends BaseMVPFragment<GanFragPresenter> implements GankFragView {

    private List<GankBean> data=new ArrayList<>();
    private List<GankMultiBean> gankMultiBeans =new ArrayList<>();
    private GankListAdapter adapter;

    public void setGankDatas(List<GankBean> data){
        this.data.addAll(data);
    }

    @Override
    protected GanFragPresenter createPresenter() {
        return new GanFragPresenter(this,lifecycleSubject);
    }

    @Override
    public Object setLayout() {
        return R.layout.wandorid_frag_gank;
    }

    @Override
    public void initUI() {
        RecyclerView rvGan = findViewById(R.id.rvGan);
        adapter=new GankListAdapter(gankMultiBeans);
        GuiHelper.initRecycleView(activity, rvGan,adapter,0);
    }

    @Override
    public void setListener() {
        super.setListener();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GankMultiBean projectBean= (GankMultiBean) adapter.getData().get(position);
                WanAndroidWebViewActivity.launch(activity,projectBean.getGankBean().getUrl());
            }
        });
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                final WandroidProjectBean projectBean= (WandroidProjectBean) adapter.getData().get(position);
                DialogHelper.builder()
                        .build()
                        .webViewShare(activity, new WebSaveListener() {
                            @Override
                            public void onWxFriends() {
                                ShareSDKManager.wxFriendsShare(projectBean.getLink());
                            }

                            @Override
                            public void onWxCircle() {
                                ShareSDKManager.wxZoneShare(projectBean.getLink());
                            }

                            @Override
                            public void onCollect() {
                                showToast("还未开放该功能");
                            }

                            @Override
                            public void onCopy() {
                                GuiHelper.copy(activity,projectBean.getLink());
                            }
                        });

                return false;
            }
        });
    }

    @Override
    public void initData() {
        mvpPresenter.loadData(data);
    }

    @Override
    public void onLoadSuccess(List<GankMultiBean> data) {
        adapter.addData(data);
    }

    @Override
    public void onDataEmpty() {
        @SuppressLint("InflateParams") View empty=getLayoutInflater().inflate(R.layout.ui_layout_empty,null);
        adapter.setEmptyView(empty);
    }
}
